package es.ats.codington.festival.daos;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;



import es.ats.codington.festival.Mapper.EventMapper;
import es.ats.codington.festival.Mapper.PlaceMapper;
import es.ats.codington.festival.Mapper.VisitorMapper;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.domain.Visitor;
import es.ats.codington.festival.exceptions.DAOSExceptions;
import es.ats.codington.festival.interfaces.daos.iPlaceDAOImpl;
import es.ats.codington.festival.services.PlaceService;
import es.ats.codington.festival.utils.FERSDataConnection.FersDC;
import es.ats.codington.festival.utils.Query.JSonQuery;


/**
 * The Class PlaceDAOImpl.
 */
public class PlaceDAOImpl implements iPlaceDAOImpl {
	
	/** The jdbc template object. */
	private JdbcTemplate jdbcTemplateObject;
	
	/** The query properties. */
	private Properties queryProperties;

	
	/**
	 * Instantiates a new place dao impl.
	 *
	 * @param jdbcTemplateObject the jdbc template object
	 * @param queryProperties the query properties
	 */
	public PlaceDAOImpl(JdbcTemplate jdbcTemplateObject,Properties queryProperties ) {
		this.jdbcTemplateObject = jdbcTemplateObject;
		this.queryProperties = queryProperties;
	} 
	

	/* (non-Javadoc)
	 * @see es.ats.codington.festival.interfaces.daos.iPlaceDAOImpl#viewPlace(int)
	 */
	@Override
	public Place viewPlace(final int idplace) throws Exception {

		Place place = new Place();
				
		List<Place> places =jdbcTemplateObject.query(queryProperties.getProperty("getidplace"),new PreparedStatementSetter(){
				
				@Override
				public void setValues(PreparedStatement statement)throws SQLException{
					statement.setInt(1,idplace);
				}
			}, new PlaceMapper());
			
		place = places.get(0);
			
		return place;
				
	}
		
			
			

	/* (non-Javadoc)
	 * @see es.ats.codington.festival.interfaces.daos.iPlaceDAOImpl#getIdsPlace(java.lang.String)
	 */
	@Override
	public ArrayList<Integer> getIdsPlace(final String type) throws Exception {

		ArrayList<Integer> idsPlaceByType= new ArrayList<Integer>();
		
		List<Place> places =jdbcTemplateObject.query(queryProperties.getProperty("idplacebytype"),new PreparedStatementSetter(){
			
			@Override
			public void setValues(PreparedStatement statement)throws SQLException{
				statement.setString(1, type);
			}
		}, new PlaceMapper());
		
		int size = places.size();
		int iter;
		for(iter=0; iter<size; iter++){
			idsPlaceByType.add(places.get(iter).getiDplace());
		}
		
		return idsPlaceByType;
	}

	/* (non-Javadoc)
	 * @see es.ats.codington.festival.interfaces.daos.iPlaceDAOImpl#createPlace(es.ats.codington.festival.domain.Place)
	 */
	@Override
	public boolean createPlace(final Place place) throws Exception {
		
		int affectedRows=0;					
			
			affectedRows =  jdbcTemplateObject.update(queryProperties.getProperty("create_place"),new PreparedStatementSetter(){
			
			@Override
			public void setValues(PreparedStatement statement)throws SQLException{
				statement.setString(1, place.getName());
				statement.setString(2, place.getAddress());
				statement.setInt(3, place.getCapacity());
				statement.setString(4, place.getTown());
				statement.setString(5, place.getDescription());
				statement.setString(6, place.getType());
				
			}
				
			});
			
			List<Place> places =jdbcTemplateObject.query(queryProperties.getProperty("select_place"),new PreparedStatementSetter(){
				@Override
				public void setValues(PreparedStatement statement)throws SQLException{
				}
			
			},new PlaceMapper());
			
			//Checking for repetitions
			int size = places.size();
			int iter;
			for (iter=0; iter<size;){
				
				if(place.getName().equals(places.get(1))&&place.getAddress().equals(places.get(2))){
					throw new Exception("This place is no longer available or it has been already created");
				}
			}
		
		if(affectedRows!=0){
			return true;
		}else{
			return false;
		}
	
		}





/* (non-Javadoc)
 * @see es.ats.codington.festival.interfaces.daos.iPlaceDAOImpl#viewPlaces()
 */
@Override
public ArrayList<Place> viewPlaces() throws Exception {
	// TODO Auto-generated method stub
	return null;
}

}

